package com.ys.peony.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.ys.peony.app.BaseApplication;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.utils.AnimUtils;
import com.ys.peony.utils.LoadingProgressUtils;

import butterknife.ButterKnife;

/**
 * Fragment基类
 *
 * @author Hunter
 */
public abstract class BaseFragment extends Fragment {

    public final static int RESPONSE_CODE = 1;
    //当前页数
    protected int currentPage = 0;

    //每页显示条目数
    protected int pageNum = 10;

    protected View mRootView;

    /**
     * 初始化布局
     */
    public abstract int getLayoutRes();

    /**
     * 初始化视图
     */
    public abstract void initView();

    public Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView != null) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
        } else {
            mRootView = inflater.inflate(getLayoutRes(), container, false);
            ButterKnife.bind(this, mRootView);
            initView();//初始化布局
        }
        return mRootView;
    }

    /**
     * 获取当前Fragment状态
     *
     * @return true为正常 false为未加载或正在删除
     */
    private boolean getStatus() {
        return (isAdded() && !isRemoving());
    }

    /**
     * 获取Activity
     *
     * @return
     */
    public BaseActivity getBaseActivity() {
        return (BaseActivity) getActivity();
    }

    @Override
    public void onStart() {
        super.onStart();
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle) {
        skipToTargetPage(clazz, bundle, SwipeDirect.DIRECT_LEFT);
    }

    /**
     * 描述：跳转到对应界面
     *
     * @param clazz
     * @param bundle
     * @param direct
     */
    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle, SwipeDirect direct) {
        skipToTargetPage(clazz, bundle, direct, false);
    }

    /**
     * @param clazz
     * @param bundle
     * @param direct
     * @param isResult ture:需要返回，false：不需要返回
     */
    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle, SwipeDirect direct, Boolean isResult) {
        skipToTargetPage(clazz, bundle, direct, isResult, RESPONSE_CODE);
    }

    /**
     * @param clazz
     * @param bundle
     * @param direct
     * @param isResult ture:需要返回，false：不需要返回
     */
    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle, SwipeDirect direct, Boolean isResult, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(BaseApplication.getInstance(), clazz);
        if (bundle == null) {
            bundle = new Bundle();
        }
        intent.putExtras(bundle);
        if (isResult) {
            this.startActivityForResult(intent, requestCode);
        } else {
            this.startActivity(intent);
        }
        switch (direct) {
            case DIRECT_UP:
                AnimUtils.bottomInTranslateAnim();
                break;
            case DIRECT_DOWN:
                AnimUtils.upInTranslateAnim();
                break;
            case DIRECT_LEFT:
                AnimUtils.inAnim();
                break;
            case DIRECT_RIGHT:
                AnimUtils.outAnim();
                break;
            case DIRECT_FADE:
                AnimUtils.fadeAlphaAnim();
                break;
            default:
                AnimUtils.inAnim();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
        hideInput();
    }

    public void hideInput() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive() && getActivity().getCurrentFocus() != null) {
                    if (getActivity().getCurrentFocus().getWindowToken() != null) {
                        imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
        });

    }
}
